﻿var titBook = titBook || {};
titBook.module = titBook.module || {};
titBook.models = titBook.models || {};
titBook.viewModels = titBook.viewModels || {};


(function (viewModels, proxy) {
    var fields = {
        Cpts: "cpts"
    };

    viewModels.CenterListViewModel = kendo.data.ObservableObject.extend({
        Cpts: "people",
        init: function (update, urlArg) {
            var me = this,
                successCallback = function (response, category) {
                    me.set("Cpts", response);
                    me.selectedCategory = category;
                    update(me);
                };

            kendo.data.ObservableObject.fn.init.call(me, {});
            proxy.sendRequest("GetCPTs", titBook.core.constants.httpVerbs.get, successCallback, "url=" + urlArg);
        },
        update: function (update, urlArg) {
            var me = this,
            successCallback = function (response) {
                me.set("Cpts", response);
                update(me);
            };
            proxy.sendRequest("GetCPTs", titBook.core.constants.httpVerbs.get, successCallback, "url=" + urlArg);
        }

    });
})(titBook.viewModels, titBook.core.proxy);

(function (models) {
    var fields = {
        CptCode: "CPTCode",
        Description: "Description"
    };

    models.CPT = kendo.data.Model.define({
        fields: {
            "Id": { type: "Number" },
            "CptCode": { type: "string" },
            "Description": { type: "string" },
        }
    });
})(titBook.models);


titBook.core.register("center-list-module", function (sandbox) {

    return {
        cptsVm: null,

        activate: function() {
        var me = this;

            router.route("/:category", function(category) {
                if (!me.cptsVm) {
                    me.cptsVm = new titBook.viewModels.CenterListViewModel(me.update, category);
                } else {
                    me.cptsVm.update(me.update, category);
                }

                //sandbox.addListener("leftMenuItemChanged", 
                //});
            });
        },
        destroy: function () {
        },

        update: function (vmArg) {
            var me = this,
                parent = $('#' + sandbox.getDomElement()),
                child,
                dataArg;

            parent.html("<div id='centerList'><div>");
            child = $("#centerList");

            dataArg = new kendo.data.DataSource({
                data: vmArg.get("Cpts")
            });
             
            child.kendoListView({
                dataSource: dataArg,
                template: "<div id='item'><p id='centerListId'>#:Id#</p><p id='centerListCptCode'>#:CptCode#</p><p id='centerListDescription'>#:Description#</p></div>",
                selectable: 'single',
                change: onChange()
            });

            function onChange() {
                window.x = $("#centerList > .k-state-selected");
                //var data = me.data.view();
                //    window.selected = $.map(this.select(), function (item) {
                //        titBook.core.Communication.notify("centerListItemChanged", data[$(item).index()].Id);
                //    });
            };

            var listView = child.data("kendoListView");
            listView.select(listView.element.children().first());
        }
    };
})




//var parent = $('#' + sandbox.getDomElement());
//parent.html("<div id='centerList'><div>");
//var child = $("#centerList");
////child.css("overflow", "scroll");
////child.css("height", screen.availHeight-10);
//var dataSource = new kendo.data.DataSource({
//    data: this.cptsVm.get('Cpts')
//});
//child.kendoListView({
//    dataSource: dataSource,
//    template: "<div>#:Id#</div>",
//    autoBind: false
//});